﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExtensionManagement.aspx.cs"
    Inherits="Paltop_v0._1.UI.Admin.ExtensionManagement" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../../Scripts/JQuery/jquery-1.7.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var jsFunctions = '<%Response.Write(jsFunctionArray);%>';
        jsFunctions = jsFunctions.split(',');
        function ShowAddPart() {
            $('#ShowAddPartBtn').replaceWith('<input id="CloseAddPartBtn" type="button" value="Close add panel" onclick="CloseAddPart()" />');
            $('#addExtensionPart').show();
            var options = '';
            for (var i = 0; i < jsFunctions.length; i++) {
                options += '<option value="' + jsFunctions[i] + '" >' + jsFunctions[i] + '</option>';
            }
            $('#jsFunctionSelectAdd').html(options);
        }
        function CloseAddPart() {
            $('#CloseAddPartBtn').replaceWith('<input id="ShowAddPartBtn" type="button" value="Add Extension" onclick="ShowAddPart()" />');
            $('#addExtensionPart').hide();
        }
        function addExtension() {
        var select=$('#jsFunctionSelectAdd')[0];
            var postData = 'ACTIONTYPE=ADMIN&ACTION=ADDEXTENTION&title=' + $('#extensionTitleInput').val() + '&jsf=' + select.options[select.selectedIndex].value;
            $.ajax({
                url: '../AjaxController.ashx',
                type: 'post',
                data: postData,
                success: function (rhtml) {
                    if (rhtml.split('$')[0] == 'ok') {
                        var item = $.parseJSON(rhtml.split('$')[1]);
                        if (item.Available == 'false') {
                            alert("ExtentionAdded!'");

                            var ExtensionRow = "<tr id=\"tr_" + item.FileExtentionID + "\"><td></td><td>" + item.Title + "</td><td>" + item.JSFunction + "</td><td>" + item.Description + "</td><td>" +
                    "<a href=\"#\" onclick=\"Edit(" + item.FileExtentionID + ")\">edit</a></td><td><a href=\"#\" onclick=\"disable(" + item.FileExtentionID + ")\">disable</a></td></tr>";
                            $('#extentionList').append(ExtensionRow);
                        }
                        else {
                            alert('Extention was already added and now is modified!');
                            $('#extentionList').find('.extTitle').each(function (index) {
                                if ($(this).html() == item.Title) {
                                    var jsf = $(this).next();
                                    jsf.html(item.JSFunction);
                                    var description = jsf.next();
                                    description.html(item.Description);
                                }
                            });
                        }
                        $('#addExtensionPart').hide();
                    }
                    else
                        alert(rhtml);
                }
            });
        }
        function Edit(id) {
            $('#EditPart').show();
            $('#savePart').html('<input  type="button" onclick="saveEdit(' + id + ')" value="Save" />');
            $('#tr_' + id + ' td').each(function (index) {
                if (index == 1)
                    $('#edit_Title').val($(this).html());
                else if (index == 2) {
                    var selectedVal = $(this).html();
                    var options = '';
                    for (var i = 0; i < jsFunctions.length; i++) {
                        options += '<option value="' + jsFunctions[i] + '" ' + (jsFunctions[i] == selectedVal ? 'selected="selected"' : '') + '>' + jsFunctions[i] + '</option>';
                    }
                    $('#JsFunctionSelect').html(options);
                }
                else if (index == 3)
                    $('#edit_Description')[0].value = $(this).html();
            });
        }
        function disable(id) {
          
            var postData = 'ACTIONTYPE=ADMIN&ACTION=DISABLEEXTENTION&eid=' + id;
            $.ajax({
                url: '../AjaxController.ashx',
                type: 'post',
                data: postData,
                success: function (rhtml) {
                    if (rhtml == 'ok') {
                        $('#tr_' + id + ' td').each(function (index) {
                            if (index == 2) {

                                $(this).html("downloadUnknownType(this.id)");

                            }
                           
                        });
                    }
                    else
                        alert(rhtml);
                }
            });
        }
        function saveEdit(id) {
            var select = $('#JsFunctionSelect')[0];
            var postData = 'ACTIONTYPE=ADMIN&ACTION=EDITEXTENTION&title=' + $('#edit_Title').val() + '&jsf=' + select.options[select.selectedIndex].value + '&decription='+$('#edit_Description').val() + '&eid='+id;
            $.ajax({
                url: '../AjaxController.ashx',
                type: 'post',
                data: postData,
                success: function (rhtml) {
                    if (rhtml == 'ok') {
                        $('#tr_' + id + ' td').each(function (index) {
                            if (index == 1)
                                $(this).html($('#edit_Title').val());
                            else if (index == 2) {
                                var selectedVal = $('#JsFunctionSelect')[0];
                                $(this).html(selectedVal.options[selectedVal.selectedIndex].value);
                               
                            }
                            else if (index == 3)
                                $(this).html($('#edit_Description')[0].value);
                        });
                    }
                    else
                        alert(rhtml);
                }
            });
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <input id="ShowAddPartBtn" type="button" value="Add Extension" onclick="ShowAddPart()" />
    <div id="addExtensionPart" style="display: none">
        <label>
            Extension Title:</label><input type="text" id="extensionTitleInput" />
        <br />
        <label>
            JS function:</label><select id="jsFunctionSelectAdd"></select>
        <input id="addExtensionBtn" onclick="addExtension()" type="button" value="Add" />
    </div>
    <div id="EditPart"  style="display: none">
    <input value="Close edit part" onclick="javascript:$('#EditPart').hide();" type="button" />
    <br />
    <label>Title:</label><input type="text" value="" id="edit_Title" />
    <br />
    <label>JsFunction:</label><select id="JsFunctionSelect"></select>
    <br />
    <label>Description:</label><textarea id="edit_Description"></textarea>
    <br />
    <div id="savePart"></div>
    </div>
    <div id="ExtensionListPart">
        <table>
            <thead>
                <tr>
                    <th>
                        ID
                    </th>
                    <th>
                        Title
                    </th>
                    <th>
                        JSFunction
                    </th>
                    <th>
                        Description
                    </th>
                    <th>
                        Edit
                    </th>
                    <th>
                        Disable
                    </th>
                </tr>
            </thead>
            <tbody id="extentionList">
                <%Response.Write(ExtensionRow); %>
            </tbody>
        </table>
    </div>
    </form>
</body>
</html>
